package com.sharemarking.wa.platform.service.OrderCommonFactory;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.Order;
import com.sharemarking.wa.common.eum.OrderOperateTypeEum;
import com.sharemarking.wa.common.eum.OrderStateEum;
import com.sharemarking.wa.platform.dto.orderCommon.OrderInfoDto;
import com.sharemarking.wa.platform.dto.orderCommon.OrderListDto;
import com.sharemarking.wa.platform.dto.orderCommon.TaskOrderListDto;
import com.sharemarking.wa.platform.dto.orderCommon.WasteListDto;
import com.sharemarking.wa.platform.mapper.*;
import com.sharemarking.wa.platform.service.SecurityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Component
public class OrderDriver implements OrderCommon{
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderCommonMapper orderCommonMapper;
    @Autowired
    private DriverVerifyMapper driverVerifyMapper;

    @Override
    public List<OrderListDto> getOrderList(Map params) {
        List<OrderListDto> list = orderCommonMapper.getList(params);

        for (OrderListDto o : list) {
            o.setWasteList(orderCommonMapper.getWasteList(o.getId()));
            o.setLoadingAddress(orderAddressMapper.getAddressByOrderId(o.getParentId(), 0));
            o.setUnLoadingAddress(orderAddressMapper.getAddressByOrderId(o.getId(), 1));

            //操作权限
            if (o.getState().equals(OrderStateEum.NO_TRANSPORT.getKey())) {
                //待运状态可以送单操作
                List<OrderOperateTypeEum> eumList = new ArrayList<>();
                eumList.add(OrderOperateTypeEum.DELIVERY);
                o.setOperate(eumList);
            }else if (o.getState().equals(OrderStateEum.DRIVER_CONFIRM.getKey())){
                //待司机确认状态可以确认操作
                List<OrderOperateTypeEum> eumList = new ArrayList<>();
                eumList.add(OrderOperateTypeEum.CONFIRM);
                o.setOperate(eumList);
            }

        }
        return list;
    }

    @Override
    public OrderInfoDto getOrderInfo(Map params) {
        String code = String.valueOf(params.get("code"));

        Order order = orderMapper.selectOne(new QueryWrapper<Order>().eq("code", code).eq("driver_id",params.get("memberId")));
        OrderInfoDto orderInfoDto = new OrderInfoDto();

        if (order!=null){
            BeanUtils.copyProperties(order,orderInfoDto);
        }

        //子订单
        //获取装货地址
        String loadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 0);
        //获取卸货地址
        String unLoadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 1);

        orderInfoDto.setLoadingAddress(loadingAddress);
        orderInfoDto.setUnLoadingAddress(unLoadingAddress);

        //完成时间 和 到达时间
        orderInfoDto.setArrivalTime(order.getArrivalTime());
        orderInfoDto.setCompleteTime(order.getCompleteTime());

        orderInfoDto.setTransferTime(order.getActualTransferTime());

        //获取经营单位名称
        if (!StringUtils.isEmpty(order.getReceiveOrgId())) {
            orderInfoDto.setOrgName(orgMapper.getOrgNameById(order.getReceiveOrgId()));
        }

        //获取经营单位联系人姓名和电话
        if (!StringUtils.isEmpty(order.getReceiveOrgMemberId())) {
            Member member = memberMapper.get(order.getReceiveOrgMemberId());
            orderInfoDto.setDriverName(member.getRealName() != null?member.getRealName():member.getNickName());
            orderInfoDto.setMobile(member.getMobile());
        }

        //获取运输单位名称
        if (!StringUtils.isEmpty(order.getTransportOrgId())) {
            orderInfoDto.setTransportOrgName(orgMapper.getOrgNameById(order.getTransportOrgId()));
        }

        //获取司机名称和电话

        orderInfoDto.setCarWeight(order.getCarWeight());
        if (!StringUtils.isEmpty(order.getDriverId())) {
            Member member = memberMapper.get(order.getDriverId());
            if (member != null && member.getRealName() != null) {
                orderInfoDto.setDriverName(member.getRealName());
            } else {
                orderInfoDto.setDriverName(member.getNickName());
            }
            orderInfoDto.setDriverMobile(member.getMobile());
            orderInfoDto.setCarNumber(driverVerifyMapper.getCarMemberIdByMemberId(order.getDriverId()));
        }

        //获取废品列表
        List<WasteListDto> childOrderWasteListDtos = orderCommonMapper.getChildOrderWasteListByOrderId(order.getId());
        orderInfoDto.setWasteList(childOrderWasteListDtos);

        Member member;
        //获取产废单位信息
        orderInfoDto.setWasteOrgName(orgMapper.getOrgNameById(order.getSendOrgId()));
        //获取产废单位联系人 手机号码
        member = memberMapper.get(order.getSendOrgMemberId());
        if (member.getRealName() != null) {
            orderInfoDto.setWasteContact(member.getRealName());
        } else {
            orderInfoDto.setWasteContact(member.getNickName());
        }
        orderInfoDto.setWasteMobile(member.getMobile());

        //获取经营单位联系人姓名和电话
        if (!StringUtils.isEmpty(order.getReceiveOrgMemberId())) {
            member = memberMapper.get(order.getReceiveOrgMemberId());
            if (member.getRealName() != null) {
                orderInfoDto.setContact(member.getRealName());
            } else {
                orderInfoDto.setContact(member.getNickName());
            }
            orderInfoDto.setMobile(member.getMobile());
        }

        //操作权限
        if (order.getState().equals(OrderStateEum.NO_TRANSPORT.getKey())) {
            //待运状态可以送单操作
            List<OrderOperateTypeEum> eumList = new ArrayList<>();
            eumList.add(OrderOperateTypeEum.DELIVERY);
            orderInfoDto.setOperate(eumList);
        }else if (order.getState().equals(OrderStateEum.DRIVER_CONFIRM.getKey())){
            //待司机确认状态可以确认操作
            List<OrderOperateTypeEum> eumList = new ArrayList<>();
            eumList.add(OrderOperateTypeEum.CONFIRM);
            orderInfoDto.setOperate(eumList);
        }

        return orderInfoDto;
    }

    @Override
    public List<TaskOrderListDto> getTaskOrderList(Map params) {

//        params.put("driverId", driverVerifyMapper.getMemberIdByDriverId((Integer) params.get("driverId")));

        List<TaskOrderListDto> list = orderCommonMapper.getTaskOrderList(params);

        for (TaskOrderListDto o : list) {
            o.setWasteList(orderCommonMapper.getWasteList(o.getId()));
            o.setLoadingAddress(orderAddressMapper.getAddressByOrderId(o.getParentId(), 0));
            o.setUnLoadingAddress(orderAddressMapper.getAddressByOrderId(o.getId(), 1));

            //设置权限
            List<OrderOperateTypeEum> eumList = new ArrayList<>();
            eumList.add(OrderOperateTypeEum.CONFIRM);
            o.setOperate(eumList);

        }
        return list;
    }

    @Override
    public OrderInfoDto getTaskOrderInfo(Map params) {
        return null;
    }
}
